﻿using LeSoft.Util;
using LeSoft.Application.Bussiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;


namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class TaskController : MvcControllerBase
    {
        private TaskBLL bll = new TaskBLL();
        private TaskUserBLL taskUserBll = new TaskUserBLL();

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetEntity(string keyValue)
        {
            var data = bll.GetEntity(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPageListJson(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 查询任务
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPageListUserJson(Pagination pagination, int State = -1, int Category = -1)
        {
            var watch = CommonHelper.TimerStart();
            var data = new object();
            if (OperatorProvider.Provider.Current().IsSystem)
            {
                data = bll.GetPageList(pagination, null).Where(t => t.State == 0);
            }
            else
            {
                var dataUser = taskUserBll.GetPageList(pagination, State, Category);
                data = bll.GetList().Join(dataUser.GroupBy(t => t.TaskId), x => x.Id, y => y.Key, (x, y) => new
                {
                    x.Id,
                    x.Name,
                   // x.TaskContent,
                    SendDate = x.SendDate != null ? x.SendDate.Value.ToString("yyyy-MM-dd") : ""
                });
            }
            if (pagination.records == 0) pagination.records = 1;
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 保存工作项目表单（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="dataItemDetailEntity">明细实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string Task, string TaskUser)
        {
            SysTaskEntity taskEntity = null; List<SysTaskUserEntity> listTaskUser = null;
            if (Task != "{}" && TaskUser != "[]")
            {
                taskEntity = Task.ToObject<SysTaskEntity>();
                listTaskUser = TaskUser.ToObject<List<SysTaskUserEntity>>();
            }
            bll.SaveForm(keyValue, taskEntity, listTaskUser);
            return Success("操作成功。");
        }
    }
}
